home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre4.z / postgre4 / src / utils / init / miscinit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-27  |  6.0 KB  |  289 lines

  1. /* ----------------------------------------------------------------
  2.  *   FILE
  3.  *    miscinit.c
  4.  *    
  5.  *   DESCRIPTION
  6.  *    miscellanious initialization support stuff
  7.  *
  8.  *   INTERFACE ROUTINES
  9.  *
  10.  *   NOTES
  11.  *
  12.  *   IDENTIFICATION
  13.  *    $Header: /private/postgres/src/utils/init/RCS/miscinit.c,v 1.7 1992/04/03 19:49:30 mao Exp $
  14.  * ----------------------------------------------------------------
  15.  */
  16.  
  17. #include "tmp/postgres.h"
  18.  
  19. #include "tmp/portal.h"        /* for EnablePortalManager, etc. */
  20. #include "utils/exc.h"        /* for EnableExceptionHandling, etc. */
  21. #include "utils/mcxt.h"        /* for EnableMemoryContext, etc. */
  22. #include "utils/log.h"
  23.  
  24. #include "tmp/miscadmin.h"
  25.  
  26. #include "catalog/pg_user.h"
  27. #include "catalog/pg_proc.h"
  28. #include "access/skey.h"
  29. #include "utils/rel.h"
  30. /*
  31.  * EnableAbortEnvVarName --
  32.  *    Enables system abort iff set to a non-empty string in environment.
  33.  */
  34. #define EnableAbortEnvVarName    "POSTGRESABORT"
  35. #define DEFAULT_PGHOME        "/usr/postgres"
  36.  
  37. typedef String    EnvVarName;
  38. extern String getenv ARGS((EnvVarName name));
  39.  
  40. /* ----------------------------------------------------------------
  41.  *        some of the 19 ways to leave postgres
  42.  * ----------------------------------------------------------------
  43.  */
  44.  
  45. /* ----------------
  46.  *    ExitPostgres
  47.  * ----------------
  48.  */
  49. void
  50. ExitPostgres(status)
  51.     ExitStatus    status;
  52. {
  53. #ifdef    __SABER__
  54.     saber_stop();
  55. #endif
  56.     exitpg(status);
  57. }
  58.  
  59. /* ----------------
  60.  *    AbortPostgres
  61.  * ----------------
  62.  */
  63. void
  64. AbortPostgres()
  65. {
  66.     String abortValue = getenv(EnableAbortEnvVarName);
  67.  
  68. #ifdef    __SABER__
  69.     saber_stop();
  70. #endif
  71.  
  72.     if (PointerIsValid(abortValue) && abortValue[0] != '\0')
  73.     abort();
  74.     else
  75.     exitpg(FatalExitStatus);
  76. }
  77.  
  78. /* ----------------
  79.  *    StatusBackendExit
  80.  * ----------------
  81.  */
  82. void
  83. StatusBackendExit(status)
  84.     int    status;
  85. {
  86.     /* someday, do some real cleanup and then call the LISP exit */
  87.     /* someday, call StatusPostmasterExit if running without postmaster */
  88.     exitpg(status);
  89. }
  90.  
  91. /* ----------------
  92.  *    StatusPostmasterExit
  93.  * ----------------
  94.  */
  95. void
  96. StatusPostmasterExit(status)
  97.     int    status;
  98. {
  99.     /* someday, do some real cleanup and then call the LISP exit */
  100.     exitpg(status);
  101. }
  102.  
  103. /* ----------------------------------------------------------------
  104.  *    processing mode support stuff (used to be in pmod.c)
  105.  * ----------------------------------------------------------------
  106.  */
  107. static ProcessingMode    Mode = NoProcessing;
  108.  
  109. bool
  110. IsNoProcessingMode()
  111. {
  112.     return ((bool)(Mode == NoProcessing));
  113. }
  114.  
  115. bool
  116. IsBootstrapProcessingMode()
  117. {
  118.     return ((bool)(Mode == BootstrapProcessing));
  119. }
  120.  
  121. bool
  122. IsInitProcessingMode()
  123. {
  124.     return ((bool)(Mode == InitProcessing));
  125. }
  126.  
  127. bool
  128. IsNormalProcessingMode()
  129. {
  130.     return ((bool)(Mode == NormalProcessing));
  131. }
  132.  
  133. void
  134. SetProcessingMode(mode)
  135.     ProcessingMode    mode;
  136. {
  137.     AssertArg(mode == NoProcessing || mode == BootstrapProcessing || mode == InitProcessing || mode == NormalProcessing);
  138.  
  139.     Mode = mode;
  140. }
  141.  
  142. /* ----------------------------------------------------------------
  143.  *    ReinitAtFirstTransaction()
  144.  *    InitAtFirstTransaction()
  145.  *
  146.  *    This is obviously some half-finished hirohama-ism that does
  147.  *    nothing and should be removed.
  148.  * ----------------------------------------------------------------
  149.  */
  150. void
  151. ReinitAtFirstTransaction()
  152. {
  153.     elog(FATAL, "ReinitAtFirstTransaction: not implemented, yet");
  154. }
  155.  
  156. void
  157. InitAtFirstTransaction()
  158. {
  159.     if (TransactionInitWasProcessed) {
  160.     ReinitAtFirstTransaction();
  161.     }
  162.     
  163.     /* Walk the relcache? */
  164.     TransactionInitWasProcessed = true;    /* XXX ...InProgress also? */
  165. }
  166.  
  167. /* ----------------------------------------------------------------
  168.  *        database path / name support stuff
  169.  * ----------------------------------------------------------------
  170.  */
  171. static String    DatabasePath = NULL;
  172. static String    DatabaseName = NULL;
  173.  
  174. String
  175. GetDatabasePath()
  176. {
  177.     return DatabasePath;
  178. }
  179.  
  180. String
  181. GetDatabaseName()
  182. {
  183.     return DatabaseName;
  184. }
  185.  
  186. void
  187. SetDatabasePath(path)
  188.     String path;
  189. {
  190.     DatabasePath = path;
  191. }
  192.  
  193. void
  194. SetDatabaseName(name)
  195.     String name;
  196. {
  197.     extern NameData MyDatabaseNameData;
  198.     
  199.     /* ----------------
  200.      *    save the database name in MyDatabaseNameData.
  201.      *  XXX we currently have MyDatabaseName, MyDatabaseNameData and
  202.      *  DatabaseName.  What uses each of these?? this duality should
  203.      *  be eliminated! -cim 10/5/90
  204.      * ----------------
  205.      */
  206.     strncpy(&MyDatabaseNameData, name, 16);
  207.     
  208.     DatabaseName = (String) &MyDatabaseNameData;
  209. }
  210.  
  211. /* ----------------------------------------------------------------
  212.  *    GetUserId and SetUserId support (used to be in pusr.c)
  213.  * ----------------------------------------------------------------
  214.  */
  215. static ObjectId    UserId = InvalidObjectId;
  216.  
  217. /* ----------------
  218.  *    GetUserId
  219.  * ----------------
  220.  */
  221. ObjectId
  222. GetUserId()
  223. {
  224.     AssertState(ObjectIdIsValid(UserId));
  225.     return (UserId);
  226. }
  227.  
  228. /* ----------------
  229.  *    SetUserId
  230.  * ----------------
  231.  */
  232.  
  233. extern Name UserRelationName;
  234. extern char     *PG_username;
  235.  
  236. void
  237. SetUserId()
  238. {
  239.     HeapScanDesc s;
  240.     ScanKeyData  key;
  241.     Relation userRel;
  242.     HeapTuple userTup;
  243.  
  244.     /*
  245.      * Don't do scans if we're bootstrapping, none of the system
  246.      * catalogs exist yet, and they should be owned by postgres
  247.      * anyway.
  248.      */
  249.     if (IsBootstrapProcessingMode())
  250.     {
  251.     UserId = getuid();
  252.     return;
  253.     }
  254.  
  255.     userRel = heap_openr(UserRelationName);
  256.     ScanKeyEntryInitialize(&key.data[0],
  257.                (bits16)0x0,
  258.                Anum_pg_user_usename,
  259.                Character16EqualRegProcedure,
  260.                (Datum)PG_username);
  261.  
  262.     s = heap_beginscan(userRel,0,NowTimeQual,1,(ScanKey)&key);
  263.     userTup = heap_getnext(s, 0, (Buffer *)NULL);
  264.     if (!HeapTupleIsValid(userTup))
  265.     {
  266.     elog(FATAL, "User %s is not in %s", PG_username, UserRelationName);
  267.     }
  268.     UserId = (ObjectId) ((Form_pg_user)GETSTRUCT(userTup))->usesysid;
  269.     heap_endscan(s);
  270.     heap_close(userRel);
  271. }
  272.  
  273. /* ----------------
  274.  *    GetPGHome
  275.  *
  276.  *  Get POSTGRESHOME from environment, or return default.
  277.  * ----------------
  278.  */
  279. char *
  280. GetPGHome()
  281. {
  282.     char *h;
  283.  
  284.     if ((h = getenv("POSTGRESHOME")) != (char *) NULL)
  285.     return (h);
  286.  
  287.     return (DEFAULT_PGHOME);
  288. }
  289.